package com.nanhai.buddhist.college.module.collegebase.service.classroomfunction;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classroomfunction.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroomfunction.ClassroomFunctionDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.collegebase.convert.classroomfunction.ClassroomFunctionConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classroomfunction.ClassroomFunctionMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 教室功能 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class ClassroomFunctionServiceImpl implements ClassroomFunctionService {

    @Resource
    private ClassroomFunctionMapper classroomFunctionMapper;

    @Override
    public String createClassroomFunction(ClassroomFunctionCreateReqVO createReqVO) {
        // 插入
        ClassroomFunctionDO classroomFunction = ClassroomFunctionConvert.INSTANCE.convert(createReqVO);
        classroomFunctionMapper.insert(classroomFunction);
        // 返回
        return classroomFunction.getId();
    }

    @Override
    public void updateClassroomFunction(ClassroomFunctionUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateClassroomFunctionExists(updateReqVO.getId());
        // 更新
        ClassroomFunctionDO updateObj = ClassroomFunctionConvert.INSTANCE.convert(updateReqVO);
        classroomFunctionMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassroomFunction(String id) {
        // 校验存在
        this.validateClassroomFunctionExists(id);
        // 删除
        classroomFunctionMapper.deleteById(id);
    }

    private void validateClassroomFunctionExists(String id) {
        if (classroomFunctionMapper.selectById(id) == null) {
            throw exception(CLASSROOM_FUNCTION_NOT_EXISTS);
        }
    }

    @Override
    public ClassroomFunctionDO getClassroomFunction(String id) {
        return classroomFunctionMapper.selectById(id);
    }

    @Override
    public List<ClassroomFunctionDO> getClassroomFunctionList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() :  classroomFunctionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ClassroomFunctionDO> getClassroomFunctionPage(ClassroomFunctionPageReqVO pageReqVO) {
        return classroomFunctionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ClassroomFunctionDO> getClassroomFunctionList(ClassroomFunctionExportReqVO exportReqVO) {
        return classroomFunctionMapper.selectList(exportReqVO);
    }

}
